package com.lzy.util;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

@Slf4j
public class ZipUtil {

    /**
     * 解压单个文件
     *
     * @param zipPath 压缩文件的路径和文件名
     * @param outfilePath 解压后的路径和文件名
     * @param fileName 所需解压的文件
     */
    public static void ZipContraFile(String zipPath, String outfilePath, String fileName) {

        try {
            // 压缩文件路径和文件名
            File file = new File(zipPath);
            // 解压后的路径和文件名
            File outFile = new File(outfilePath);
            // 解压文件
            ZipFile zipFile = new ZipFile(file);
            // 所解压的文件名
            ZipEntry entry = zipFile.getEntry(fileName);

            InputStream input = zipFile.getInputStream(entry);
            OutputStream output = new FileOutputStream(outFile);

            int temp = 0;
            while ((temp= input.read())!=-1) {
                output.write(temp);
            }
            input.close();
            output.close();
        } catch (Exception e) {
            log.error(e.toString());
        }
    }
}
